package com.dns.customers.controller;

import com.dns.customers.AppPage;
import com.dns.customers.DnsTask;
import com.dns.customers.model.*;
import com.dns.customers.service.CustomerDnsService;
import com.dns.customers.service.DomainService;
import com.dns.framework.data.Test1;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.poi.util.SystemOutLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dns")
public class CustomerController extends AppPage {
    //管理服务
    @Autowired
    CustomerDnsService customerDnsService;
    //域名自服务
    @Autowired
    DomainService domainService;

    /**
     * 创建大客户
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/createCustAccount",method=RequestMethod.POST,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String addCustomerAccount(HttpServletRequest request) throws Exception {
        return customerDnsService.createCustAccount(request);
    }

    //修改大客户
    @RequestMapping(value = "/modifyCusotmerAccount",method=RequestMethod.POST,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String modifyCusotmerAccount(HttpServletRequest request,@RequestParam(value = "customers") String customers) throws Exception {
        return customerDnsService.modifyCustomerAccount(request,customers);
    }
    /**
     * 删除大客户或删除根域名
     * @param customers
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/deleteCusotmerAccount",method=RequestMethod.POST,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String delCustomerAccount(HttpServletRequest request,@RequestParam(value = "customers") String customers ) throws Exception {
        return customerDnsService.delCustomerAccount(request,customers );
    }

    /**
     * 分页查询大客户列表
     * @param request
     * @return
     */
    @RequestMapping(value="/queryCustListByName",method =RequestMethod.GET,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String queryCustListByName(HttpServletRequest request){
        String  result = customerDnsService.queryCustListByName(request);
        return  result;
    }
    /*
     * 查询大客户信息（包含域名信息）
     * @param queryDns
     * @return
     */
    @RequestMapping(value="/queryAccoundDomain",method =RequestMethod.GET,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String queryAccoundDomain(@RequestParam(value = "custId")String custId){
        return  customerDnsService.queryAccoundDomain(custId);
    }
    /**
     * 分页查询根域名详情
     * @param custId 客户id
     * @return
     */
    @RequestMapping(value="/queryRootDomainPage",method =RequestMethod.GET,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String queryRootDomainPage(String  custId,int currPage,int pageSize){
        return  customerDnsService.queryRootDomainPage(custId,currPage,pageSize);
    }

//*****************************************************管理端结束*****************************************************************
//*****************************************************自服务开始*****************************************************************

    /**
     * 查询所有域名类型
     * @return
     */
    @RequestMapping(value="/getDnsResTypes",method =RequestMethod.GET,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String getDnsResTypes(){
        return  customerDnsService.getDnsResTypes();
    }
    /**
     * 查询大客户账户信息
     * @param custId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryCustomerAccount",method=RequestMethod.GET,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String queryCustomerAccount(@RequestParam(value = "custId") String custId) throws Exception {
        return customerDnsService.queryCustomerAccount(custId);
    }
    /**
     * 根据客户ID查询根域名和子域名及其解析类型
     * @param custId,
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryDomainnameAndType",method=RequestMethod.GET,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String queryDomainnameAndType(@RequestParam(value = "custId") String custId) throws Exception {
        return customerDnsService.queryDomainnameAndType(custId);
    }
    /**
     * 根据客户ID查询对应的订单信息
     */
    @RequestMapping(value="/getOrderInfoByCustId",method =RequestMethod.GET,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String getOrderInfoByCustId(@RequestParam String custId){
        return customerDnsService.getOrderInfoByCustId(custId);
    }

    /**
     * 根据客户ID和域名类型查询客户归属的域名信息
     * @param custId, domain_name
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryDomainnameByIDAndType",method=RequestMethod.GET,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String queryDomainnameByIDAndType(@RequestParam(value = "custId") String custId, @RequestParam(value = "domain_type") String domain_type){
        return customerDnsService.queryDomainnameByIDAndType(custId, domain_type);
    }
    /**
     * 根据客户ID分页查询域名列表
     * @param request,
     * @return
     */
    @RequestMapping(value = "/queryRecordInfo",method=RequestMethod.GET,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String queryRecordInfo(HttpServletRequest request){
        return customerDnsService.queryRecordInfo(request);
    }
    /**
     * 新增和修改域名信息
     * @return
     */
    @RequestMapping(value = "/addCustChildDomain",method = RequestMethod.POST,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String  addCustomerConfig(HttpServletRequest request) {
        String result = domainService.addCustDomain(request);
        return  result;
    }
    /*
     * 批量删除域名记录信息
     * @param queryDns
     * @return
     */
    @RequestMapping(value="/delDomainRecordList",method =RequestMethod.POST,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String delDomainRecordList(HttpServletRequest req){
        //从请求中取出参数
        InputStream is = null;
        String result ="";
        try {
            is = req.getInputStream();
            String contentStr = IOUtils.toString(is, "utf-8");
            log.debug("contentStr: "+contentStr);
            result = domainService.delDomainRecordList(contentStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  result;
    }
    /**
     * 根据多个条件分页查询订单
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryOrder",method=RequestMethod.GET,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String queryOrder(HttpServletRequest request) throws Exception {
        return customerDnsService.queryOrder(request);
    }
    /**
     * 查看订单详情
     * @param orderId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryOrderMore",method=RequestMethod.GET,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String queryOrderMore(@RequestParam(value = "orderId") String orderId) throws Exception {
        return customerDnsService.queryOrderMore(orderId);
    }
    /**
     * 订单详情返回结果明细
     * @param request
     * @return
     */
    @RequestMapping(value="/queryOrderDetail",method =RequestMethod.GET,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String queryOrderDetail(HttpServletRequest request){
        return  domainService.queryOrderDetail(request);
    }
    /**
     * 查询所有省份信息
     */
    @RequestMapping(value="/getAllProvinces",method =RequestMethod.GET,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String getAllProvinces(){
        return  customerDnsService.getAllProvinces();
    }
    /**
     * 下发订单任务页面，查询根域名下拉
     * @param custId 客户id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryRootDomainName",method=RequestMethod.GET,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String queryRootDomainName(@RequestParam(value = "custId") String custId) throws Exception {
        return customerDnsService.queryRootDomainName(custId);
    }
    /**
     * 下发订单任务,包含域名解析和缓存清除
     * 下发之前，先校验本地数据库是否有在途订单，若有在途单，则提示无法下发.
     * 目的是等待定时器将上一个订单任务解析结果入到我们的订单明细表dns_order_detail
     * @param params
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addOrderTask",method=RequestMethod.POST,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String addOrderTask(@RequestParam(value = "params") String params)  {
        return domainService.addOrderTask(params);
    }
    /**
     * 下发解析任务用来查询缓存清除任务的结果
     */
    @RequestMapping(value="/checkCacheTaskResult",method =RequestMethod.GET,produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String checkCacheTaskResult(String custId,String orderId,String domainName,String type){
        return  domainService.checkCacheTaskResult(custId,orderId,domainName,type);
    }

}